﻿using Lanbt.Core.Entity.EntityDB;
using System.Linq.Expressions;

namespace Lanbt.Core.Repository.SqlService.IRepository
{
    public interface IRepositoryBase<T> where T : EntityBase,new()
    {

        #region 查询

        /// <summary>
        /// 查询数据条数
        /// </summary>
        /// <returns></returns>
        Task<int> Count(bool isAll = false);

        /// <summary>
        /// 查询数据条数
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task<int> Count(Expression<Func<T,bool>> where, bool isAll = false);

        /// <summary>
        /// 获取第一条数据
        /// </summary>
        /// <param name="isAll">是否包括删除数据</param>
        /// <returns></returns>
        Task<T> FirstOrDefault(bool isAll = false);

        /// <summary>
        /// 获取第一条数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="isAll">是否包括删除数据</param>
        /// <returns></returns>
        Task<T> FirstOrDefault(string id, bool isAll = false);

        /// <summary>
        /// 获取第一条数据
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task<T> FirstOrDefault(Expression<Func<T, bool>> where, bool isAll = false);

        /// <summary>
        /// 获取第一条数据（倒序）
        /// </summary>
        /// <param name="order">排序</param>
        /// <returns></returns>
        Task<T> FirstOrDefaultOrderByDesc(Expression<Func<T, object>> order);

        /// <summary>
        /// 获取第一条数据（倒序）
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="order">排序</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task<T> FirstOrDefaultOrderByDesc(Expression<Func<T, bool>> where, Expression<Func<T, object>> order, bool isAll = false);

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        Task<IList<T>> GetAll(bool isAll = false);

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task<IList<T>> Query(Expression<Func<T, bool>> where, bool isAll = false);

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="order">排序字段</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task<IList<T>> Query(Expression<Func<T, bool>> where, Expression<Func<T, object>> order, bool isAll = false);

        /// <summary>
        /// 查询数据（倒序）
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <param name="order">排序字段</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task<IList<T>> QueryOrderByDesc(Expression<Func<T, bool>> where, Expression<Func<T, object>> order, bool isAll = false);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">显示条数</param>
        /// <returns></returns>
        Task<Tuple<int, IList<T>>> QueryToPageList(int pageIndex,int pageSize);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">显示条数</param>
        /// <param name="where">条件</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task<Tuple<int, IList<T>>> QueryToPageList( int pageIndex,int pageSize,Expression<Func<T, bool>> where, bool isAll = false);

        /// <summary>
        /// 分页查询（正序）
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">显示条数</param>
        /// <param name="where">条件</param>
        /// <param name="order">排序</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task<Tuple<int, IList<T>>> QueryToPageListOrderBy(int pageIndex,int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, object>> order, bool isAll = false);

        /// <summary>
        /// 分页查询（倒序）
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">显示条数</param>
        /// <param name="where">条件</param>
        /// <param name="order">排序</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task<Tuple<int, IList<T>>> QueryToPageListOrderByDesc(int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, object>> order, bool isAll = false);

        #endregion

        #region 增删改

        /// <summary>
        /// 单个添加
        /// </summary>
        /// <param name="entity">主键集合</param>
        /// <returns></returns>
        Task<string> InsertOne(T entity);
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entitys">实体集合</param>
        /// <returns></returns>
        Task<int> InsertMany(IList<T> entitys);
        /// <summary>
        /// 大数据添加
        /// </summary>
        /// <remarks>数据越多，速度越快</remarks>
        /// <param name="entities">实体集合</param>
        /// <returns></returns>
        Task<int> BulkInsert(IList<T> entitys);

        /// <summary>
        /// 单个修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        Task<int> UpdateOne(T entity);
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entitys">实体集合</param>
        /// <returns></returns>
        Task<int> UpdateMany(IList<T> entitys);
        /// <summary>
        /// 大数据修改
        /// </summary>
        /// <remarks>数据越多，速度越快</remarks>
        /// <param name="entitys">实体集合</param>
        /// <returns></returns>
        Task<int> BulkUpdate(IList<T> entitys);

        /// <summary>
        /// 假删除-根据主键ID
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        Task Delete_false(string id);
        /// <summary>
        /// 假删除-根据主键集合
        /// </summary>
        /// <param name="ids">主键集合</param>
        /// <returns></returns>
        Task Delete_false(IList<string> ids);

        /// <summary>
        /// 假删除-根据实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task Delete_false(T entity);

        /// <summary>
        /// 假删除-根据实体集合
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        Task Delete_false(IList<T> entitys);

        /// <summary>
        /// 假删除-根据自定义条件
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task Delete_false(Expression<Func<T, bool>> where, bool isAll = false);

        /// <summary>
        /// 真删除-根据主键
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        Task Delete_true(string id);

        /// <summary>
        /// 真删除-根据主键集合
        /// </summary>
        /// <param name="ids">主键集合</param>
        /// <returns></returns>
        Task Delete_true(IList<string> ids);

        /// <summary>
        /// 真删除-根据实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        Task Delete_true(T entity);

        /// <summary>
        /// 真删除-根据实体集合
        /// </summary>
        /// <param name="entitys">实体集合</param>
        /// <returns></returns>
        Task Delete_true(IList<T> entitys);

        /// <summary>
        /// 真删除-根据自定义条件
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="isAll">是否全部</param>
        /// <returns></returns>
        Task Delete_true(Expression<Func<T,bool>> where, bool isAll = false);

        /// <summary>
        /// 清空表
        /// </summary>
        /// <returns></returns>
        Task<bool> DropTable();

        #endregion
    }
}
